package com.yishe.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

/**
 * create by: chusifeng
 * Create in 2021/4/9 13:49
 * Description:AES加密类
 * <p>Example</p>
 */
@Slf4j
public class AESBase64Util {
	public static final String TRANS_FORMATION = "AES/ECB/PKCS5Padding";
	public static final String ENCODE_UTF_8 = "UTF-8";
	/** 默认秘钥，只用于测试环境:  abcdefg123456789 **/
	public static final String DEFAULT_KEY = "abcdefg123456789";
	

	/**
	 * 
	 * encrypt:(字符串加密). <br/>
	 * TODO(这里描述这个方法的注意事项 – 可选).<br/>
	 * @param inputStr 需要加密的秘钥字符串
	 * @param key 加密使用的私密串
	 *
	 * @author 蒋万勇
	 * @since 2019-12-20
	 */
	public static String encrypt(String inputStr, String key) throws Exception {
		String encryptKey = getDefaultKey(key);
		String encryptString = inputStr;
		if (encryptKey == null) {
			log.warn("encryptKey为空null，不允许操作！");
			return null;
		}
		// 判断Key是否为16位
		System.out.println(encryptKey.length());
		if (encryptKey.length() != 16) {
			log.warn("Key长度不是16位，不允许操作！");
			return null;
		}
		byte[] raw = encryptKey.getBytes(ENCODE_UTF_8);
		SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
		Cipher cipher = Cipher.getInstance(TRANS_FORMATION);// "算法/模式/补码方式"
		cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
		byte[] encrypted = cipher.doFinal(encryptString.getBytes(ENCODE_UTF_8));
		return new Base64().encodeToString(encrypted);

	}

	/**
	 * decrypt:(字符串解密 ). <br/>
	 * TODO(这里描述这个方法的注意事项 – 可选).<br/>
	 * @param inputStr 需要解密的秘钥字符串
	 * @param key 解密使用的私密串
	 * @author 蒋万勇57
	 * @since 2019-12-20
	 */
	public static String decrypt(String inputStr, String key) throws Exception {

		String decryptKey = getDefaultKey(key);
		String decryptString = inputStr;
		try {
			// 判断Key是否正确
			if (decryptKey == null) {
				log.warn("Key为空null，不允许操作！");
				return null;
			}
			// 判断Key是否为16位
			if (decryptKey.length() != 16) {
				log.warn("Key长度不是16位，不允许操作！");
				return null;
			}
			byte[] raw = decryptKey.getBytes(ENCODE_UTF_8);
			SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
			Cipher cipher = Cipher.getInstance(TRANS_FORMATION);
			cipher.init(Cipher.DECRYPT_MODE, skeySpec);
			byte[] encrypted1 = new Base64().decode(decryptString);// 先用base64解密

			byte[] original = cipher.doFinal(encrypted1);
			String originalString = new String(original, ENCODE_UTF_8);
			return originalString;
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}

	/**
	 * 
	 * getDefaultKey:(获取默认秘钥信息). <br/>
	 * TODO(这里描述这个方法的注意事项 – 可选).<br/>
	 *
	 * @author 蒋万勇57
	 * @since 2019-12-20
	 */
	public static String getDefaultKey(String key) {
		String retKey = "";
		if ("".equals(key) || key == null) {
			// 赋值默认秘钥
			retKey = DEFAULT_KEY;
			log.warn("当前使用默认秘钥串，生产环境不允许这么操作，请注意!  retKey:{}",retKey);
		} else {
			retKey = key;
		}
		return retKey;
	}

	public static void main(String[] args) throws Exception {
		
		String enStr = AESBase64Util.encrypt("Password#7", DEFAULT_KEY);
		log.info("加密秘钥为: {}",enStr);
		
		String deStr = AESBase64Util.decrypt("PSPNODUIoZoz4TY0LmJaaQ==", DEFAULT_KEY);
		log.info("解密秘钥为: {}",deStr);
	}
}
